package cz.zcu.fav.ratdroid.activity;

import org.joda.time.LocalDateTime;

import cz.zcu.fav.ratdroid.R;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import cz.zcu.fav.ratdroid.activity.calendar.CalendarActivity;
import cz.zcu.fav.ratdroid.activity.calendar.day.DayActivity;
import cz.zcu.fav.ratdroid.activity.calendar.provider.ICalendarDataProvider;
import cz.zcu.fav.ratdroid.activity.calendar.provider.ObjectDataProvider;
import cz.zcu.fav.ratdroid.activity.calendar.reservation.CreatePickUpReservationActivity;
import cz.zcu.fav.ratdroid.bean.Object;
import cz.zcu.fav.ratdroid.bean.Objects;
import cz.zcu.fav.ratdroid.bean.Reservations;
import cz.zcu.fav.ratdroid.bean.User;
import cz.zcu.fav.ratdroid.service.IRestListener;
import cz.zcu.fav.ratdroid.service.RestService;

/**
 * Activity shows object credentials and corresponding actions.
 * 
 * @author Lukas Gemela
 * 
 */
public class ObjectProfileActivity extends SkeletonRestActivity<Objects>
{
	public static int OBJECT_INVALIDATED_REQUEST_CODE = 1;

	// selected object
	private Object obj;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);

		// recreated activity
		if (savedInstanceState != null)
		{
			obj = (Object) savedInstanceState
					.getSerializable(Object.SINGLE_OBJECT_IDENTIFIER);
		} else
		{
			obj = (Object) getIntent().getSerializableExtra(
					Object.SINGLE_OBJECT_IDENTIFIER);
		}

		setContentView(R.layout.object_profile_layout);
		TextView objClassValue = (TextView) findViewById(R.id.objectClassValue);
		TextView objDescValue = (TextView) findViewById(R.id.objectDescriptionValue);
		TextView objNameValue = (TextView) findViewById(R.id.objectNameValue);
		TextView pickable = (TextView) findViewById(R.id.pickableLabel);
		TextView returnable = (TextView) findViewById(R.id.returnableLabel);

		// bind all data to view
		pickable.setVisibility(obj.isPickable() ? View.VISIBLE : View.INVISIBLE);
		returnable.setVisibility(obj.isReturnable() ? View.VISIBLE
				: View.INVISIBLE);

		objClassValue.setText(obj.getObjectClassName());
		objDescValue.setText(obj.getDescription());
		objNameValue.setText(obj.getName());
	}

	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.object_profile_menu, menu);

		// do not show favourite action button if object is already in favourite
		// list
		// (and vice versa)
		if (!obj.isFavourite())
		{
			menu.removeItem(R.id.favouriteButton);
		} else
		{
			menu.removeItem(R.id.unfavouriteButton);
		}
		if (!obj.isPickable())
		{
			menu.removeItem(R.id.PickUpObjButton);
		}
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.favouriteButton:
			setRestTask(RestService.getInstance().removeFavourite(this,
					obj.getId(), this));
			break;
		case R.id.unfavouriteButton:
			setRestTask(RestService.getInstance().addFavourite(this,
					obj.getId(), this));
			break;
		case R.id.calendarObjButton:
			// create new intend, put corresponding data provider in it and
			// start callendar activity
			Intent cal = new Intent(this, CalendarActivity.class);
			cal.putExtra(
					ICalendarDataProvider.CALENDAR_DATA_PROVIDER_IDENTIFIER,
					new ObjectDataProvider(obj, getSharedPreferences(
							User.SINGLE_USER_IDENTIFIER, MODE_PRIVATE).getInt(
							User.USER_ID, -1)));
			startActivity(cal);
			break;
		case R.id.PickUpObjButton:
			RestService.getInstance().pickUpObject(obj.getId(), this,
					new IRestListener<Reservations>()
					{
						// at first we need to be suer if it is possible toé
						// pick up objekt.
						// if not, we can create a create reservation activity,
						// where user can
						// make a reservation.
						// otherwise just toast a message
						public void notifyDataLoaded(Reservations data)
						{
							if (!data.getReservations().isEmpty())
							{
								runOnUiThread(new Runnable()
								{
									public void run()
									{
										Toast.makeText(
												ObjectProfileActivity.this,
												ObjectProfileActivity.this
														.getString(R.string.reservationPicked),
												Toast.LENGTH_SHORT).show();
									}
								});
							} else
							{
								Intent intent = new Intent(
										ObjectProfileActivity.this,
										CreatePickUpReservationActivity.class);
								intent.putExtra(
										Object.SINGLE_OBJECT_IDENTIFIER, obj);
								LocalDateTime selectedTime = LocalDateTime
										.now();
								intent.putExtra(
										DayActivity.PRESELECTED_DATETIME,
										selectedTime);
								startActivity(intent);
							}
						}

						public void notifyException(final Exception ex)
						{
							runOnUiThread(new Runnable()
							{

								public void run()
								{
									Toast.makeText(ObjectProfileActivity.this,
											ex.getMessage(), Toast.LENGTH_SHORT)
											.show();
								}
							});
						}
					});

			break;
		default:
			break;
		}
		return true;
	}

	@Override
	protected void onSaveInstanceState(Bundle outState)
	{
		super.onSaveInstanceState(outState);
		outState.putSerializable(Object.SINGLE_OBJECT_IDENTIFIER, obj);
	}

	@Override
	public void finish()
	{
		Intent returnIntent = new Intent();
		returnIntent.putExtra(Object.SINGLE_OBJECT_IDENTIFIER, obj);
		setResult(RESULT_OK, returnIntent);
		super.finish();
	}

	public void notifyDataLoaded(Objects data)
	{
		super.notifyDataLoaded(data);
		if (data != null && data.getObjects().size() >= 1)
		{
			obj.setFavourite(data.getObjects().get(0).isFavourite());
		}
		invalidateOptionsMenu();
	}

}
